package com.shop.shoporder.controller.DataReport;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.entity.DataReport.cqtlfg;
import com.shop.shoporder.entity.order.Order;
import com.shop.shoporder.mapper.eventdetailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value = "/DataReport")
public class qtlfgcController {

    @Autowired
    eventdetailsMapper eventdetailsMapper1;

    @ResponseBody
    @RequestMapping(value = "/getFgj")
    public Response getFgj(HttpServletRequest request){

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        String endTime =formatter.format(c.getTime());
        System.out.println("endTime:"+endTime);

        //读取数据表
        List<cqtlfg> cqtlfgList = eventdetailsMapper1.selectNumBy(endTime);
        int sum = eventdetailsMapper1.selectSum(endTime);
        System.out.println("Sum:"+sum);
        return new Response(MessageConstants.MSG_SUCCESS, cqtlfgList, sum, "", request);
    }

    @ResponseBody
    @RequestMapping(value = "/List", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response List(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                Calendar c = Calendar.getInstance();
                String endTime =formatter.format(c.getTime());
                System.out.println("endTime:"+endTime);


                //读取数据表
                List<cqtlfg> cqtlfgList = eventdetailsMapper1.selectNumBy(endTime);
                if (cqtlfgList != null&&cqtlfgList.size()>0) {
                    int sum = eventdetailsMapper1.selectSum(endTime);
                    System.out.println("Sum:"+sum);
                    return new Response(MessageConstants.MSG_SUCCESS, cqtlfgList, sum, "", request);
                } else {
                    return new Response(MessageConstants.MSG_SUCCESS, null, "没有数据", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误1", request);
            }
        }
    }

    @ResponseBody
    @RequestMapping(value = "/Self", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response Self(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {

                String res = map.get("parm").toString();
                System.out.println(res);

                JSONObject jsonRes = JSONObject.parseObject(res);

                String startTime = jsonRes.get("startTime").toString();
                String endTime = jsonRes.get("endTime").toString();

                System.out.println("startTime:"+startTime);
                System.out.println("endTime:"+endTime);

                //读取数据表
                List<cqtlfg> cqtlfgList = eventdetailsMapper1.selectNumByDay(startTime,endTime);
                if (cqtlfgList != null&&cqtlfgList.size()>0) {
                    int sum = eventdetailsMapper1.selectSumDay(startTime,endTime);
                    System.out.println("Sum:"+sum);
                    return new Response(MessageConstants.MSG_SUCCESS, cqtlfgList, sum, "", request);
                } else {
                    return new Response(MessageConstants.MSG_SUCCESS, null, "没有数据", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误1", request);
            }
        }
    }

}
